package com.maxwell.googleplay.fragment;

import java.util.List;
import java.util.Random;

import com.maxwell.googleplay.R;
import com.maxwell.googleplay.base.BaseFragment;
import com.maxwell.googleplay.base.LoadingPager.LoadedResult;
import com.maxwell.googleplay.protocol.HotProtocol;
import com.maxwell.googleplay.utils.UIUtils;
import com.maxwell.googleplay.views.FlowLayout;

import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.StateListDrawable;
import android.os.Bundle;
import android.os.SystemClock;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

/**
 * @author yue
 * @创建时间 2016-3-30上午9:26:27
 * @描述 TODO
 * 
 */
public class HotFragment extends BaseFragment {

	private List<String> mDatas;

	@Override
	public LoadedResult initData() {
		HotProtocol protocol = new HotProtocol();
		try {
			mDatas = protocol.loadData(0);
			checkState(mDatas);
			return LoadedResult.SUCCESS;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return LoadedResult.ERROR;
		}

	}

	@Override
	public View initSuccessView() {
		// 返回成功的视图
		ScrollView scrollView = new ScrollView(UIUtils.getContext());
		// 创建流式布局
		FlowLayout fl = new FlowLayout(UIUtils.getContext());
		// 往流式布局添加孩子
		for (final String data : mDatas) {
			TextView tv = new TextView(UIUtils.getContext());

			tv.setText(data);
			tv.setTextColor(Color.WHITE);
			tv.setTextSize(16);
			// tv.setBackgroundResource(R.drawable.shape_hot_fl_tv);

			GradientDrawable background = new GradientDrawable();
			// 设置填充颜色
			// 拿到随机颜色
			Random random = new Random();

			int alpha = 255;
			int red = random.nextInt(190) + 30;// 0~255之间随机取值,但考虑到太暗或太亮，在30~220之间取
			int green = random.nextInt(190) + 30;// 0~255之间随机取值
			int blue = random.nextInt(190) + 30;// 0~255之间随机取值

			int argb = Color.argb(alpha, red, green, blue);

			background.setColor(argb);
			// 设置圆角半径
			background.setCornerRadius(UIUtils.dip2px(6));

			//设置状态图片(代码设置Selector)
			StateListDrawable stateListDrawable = new StateListDrawable();
			GradientDrawable pressedDrawable = new GradientDrawable();
			pressedDrawable.setColor(Color.DKGRAY);
			pressedDrawable.setCornerRadius(UIUtils.dip2px(6));
			//这两行代码的顺序和在XML中定义中是一样的，顺序不能变
			stateListDrawable.addState(new int[]{android.R.attr.state_pressed} , pressedDrawable);
			stateListDrawable.addState(new int[]{} , background);
			
			
			tv.setBackgroundDrawable(stateListDrawable);
			int padding = UIUtils.dip2px(5);
			tv.setPadding(padding, padding, padding, padding);
			tv.setGravity(Gravity.CENTER);
			
			tv.setClickable(true);
			tv.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					Toast.makeText(UIUtils.getContext(), data, 0).show();
					
				}
			});
			
			fl.addView(tv);
		}

		scrollView.addView(fl);
		return scrollView;

	}
}
